package com.bdmc.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.base.domain.bo.BaseDeviceBo;
import com.bdmc.base.domain.vo.BaseDeviceVo;
import com.bdmc.base.domain.BaseDevice;
import com.bdmc.base.mapper.BaseDeviceMapper;
import com.bdmc.base.service.IBaseDeviceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备管理Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Service
public class BaseDeviceServiceImpl extends ServicePlusImpl<BaseDeviceMapper, BaseDevice, BaseDeviceVo> implements IBaseDeviceService {

    @Override
    public BaseDeviceVo queryById(Long deviceId){
        return getVoById(deviceId);
    }

    @Override
    public TableDataInfo<BaseDeviceVo> queryPageList(BaseDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseDevice> lqw = buildQueryWrapper(bo);
        Page<BaseDeviceVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<BaseDeviceVo> mySelectPage(BaseDeviceBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<BaseDeviceVo> queryList(BaseDeviceBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    @Override
    public BaseDeviceVo getOneVoByImei(String imei) {
        LambdaQueryWrapper<BaseDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(BaseDevice::getImei,imei).last("limit 1");
        return getVoOne(lqw);
    }

    private LambdaQueryWrapper<BaseDevice> buildQueryWrapper(BaseDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, BaseDevice::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), BaseDevice::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceType()), BaseDevice::getDeviceType, bo.getDeviceType());
        lqw.eq(StringUtils.isNotBlank(bo.getDevicePhoneNum()), BaseDevice::getDevicePhoneNum, bo.getDevicePhoneNum());
        lqw.eq(StringUtils.isNotBlank(bo.getSimNum()), BaseDevice::getSimNum, bo.getSimNum());
        lqw.eq(StringUtils.isNotBlank(bo.getImei()), BaseDevice::getImei, bo.getImei());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BaseDeviceBo bo) {
        BaseDevice add = BeanUtil.toBean(bo, BaseDevice.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setDeviceId(add.getDeviceId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BaseDeviceBo bo) {
        BaseDevice update = BeanUtil.toBean(bo, BaseDevice.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BaseDevice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
